<script language="javascript" type="text/javascript">
var quyen_id="";
var quyenun="";

function quyen_a()
{
	var pdata='';
	pdata=pdata+'&un='+encodeURI($("#quyen_a_table #un").val());
	pdata=pdata+'&mamod='+encodeURI(properStr($("#quyen_a_table #mamod").val()));
	pdata=pdata+'&select_r='+encodeURI(properStr($("#quyen_a_table #select_r").val()));
	pdata=pdata+'&insert_r='+encodeURI($("#quyen_a_table #insert_r").val());
	pdata=pdata+'&update_r='+encodeURI($("#quyen_a_table #update_r").val());
	pdata=pdata+'&delete_r='+encodeURI($("#quyen_a_table #delete_r").val());
	
	$("#fquyen_add").dialog("option","disabled",true);
	$.ajax({
		url:"modules/quyen/a.php",
		type:"POST",
		dataType:"xml",
		data:pdata,
		success:function(xml){
			var status=$(xml).find("status").text();
			var message=$(xml).find("message").text();
			msgbox(status,message);
		
			if(status=="ok") 
			{
				$("#fquyen_add").dialog("close");
				quyen_id=$(xml).find("id").text()
			}
			else $("#fquyen_add").dialog("option","disabled",false);
		}
	});
}
//-----------------------------------------------------------------------------------------
function quyen_af()
{
	var fquyenoptions={
		width:520,
		title:"Thêm quyền hạn ",
		modal:true,
		buttons:{
			"Thêm":quyen_a,
			"Hủy":function(){$("#fquyen_add").dialog("close");}
		},
		disabled:true,
		focus:quyen_af_ah,
		open:rebuildACL,
		close:function(){
			$("#fquyen_add").remove();
			$("#quyen_flex").flexReload();
			quyen_af_ah();
		}
	};
	
	$("body").append("<div id=\"fquyen_add\" style=\"display:none;\"></div>");
	$("#fquyen_add").dialog(fquyenoptions);		
	$("#fquyen_add").load("modules/quyen/af.php",'',function(){
		$("#fquyen_add").dialog("option","disabled",false);
		$("#fquyen_add").dialog("option","position","center");
	});
}
//-----------------------------------------------------------------------------------------
function quyen_u()
{
	var pdata='';
	pdata=pdata+'&un='+encodeURI($("#quyen_u_table #un").val());
	pdata=pdata+'&mamod='+encodeURI(properStr($("#quyen_u_table #mamod").val()));
	pdata=pdata+'&select_r='+encodeURI(properStr($("#quyen_u_table #select_r").val()));
	pdata=pdata+'&insert_r='+encodeURI($("#quyen_u_table #insert_r").val());
	pdata=pdata+'&update_r='+encodeURI($("#quyen_u_table #update_r").val());
	pdata=pdata+'&delete_r='+encodeURI($("#quyen_u_table #delete_r").val());
	
	$("#fquyen_update").dialog("option","disabled",true);
	
	$.ajax({
		url:"modules/quyen/u.php",
		type:"POST",
		dataType:"xml",
		data:pdata,
		success:function(xml){
			var status=$(xml).find("status").text();
			var message=$(xml).find("message").text();
			msgbox(status,message);
			if(status=="ok") $("#fquyen_update").dialog("close");
			else $("#fquyen_update").dialog("option","disabled",false);
		}
	});
}
//-----------------------------------------------------------------------------------------
function quyen_uf()
{
	if(!$("#quyen_flex .trSelected").attr("id"))
	{
		msgbox("fail","Phải chọn một mục để cập nhật !");
		return;
	}
	var sma=$("#quyen_flex .trSelected").attr("id");
	sma = sma.substring(sma.lastIndexOf("row")+3);
	quyen_id=sma;
	var mamod=$("#quyen_flex .trSelected>td:nth-child(2)>div").html();
	
	$.ajax({
		url:"modules/quyen/s.php",
		type:"POST",
		dataType:"xml",
		data:'un='+quyenun+'&mamod='+mamod,
		success:function(xml){
			
			var k=$(xml).find("row");
			var fquyenoptions={
						width:320,
						title:"Cập nhật quyền hạn ",
						modal:true,
						buttons:{
							"Cập nhật":quyen_u,
							"Hủy":function(){$("#fquyen_update").dialog("close");}
						},
						disabled:true,
						focus:quyen_uf_ah,
						open:rebuildACL,
						close:function(){
							$("#fquyen_update").remove();
							$("#quyen_flex").flexReload();
							quyen_uf_dh();
						}
					};

					$("body").append("<div id=\"fquyen_update\" style=\"display:none;\"></div>");
					$("#fquyen_update").dialog(fquyenoptions);
					$("#fquyen_update").load("modules/quyen/uf.php","",function(){
						$("#quyen_u_table #id").attr("value",sma);
						$("#quyen_u_table #un").attr("value",quyenun);
						
				
						if(k[0].childNodes[1].text) 
						{
							$("#quyen_u_table #tenmod").html(k[0].childNodes[6].text);
							$("#quyen_u_table #mamod").attr("value",k[0].childNodes[1].text);
						}
						else 
						{
							$("#quyen_u_table #tenmod").html(k[0].childNodes[6].textContent);
							$("#quyen_u_table #mamod").attr("value",k[0].childNodes[1].textContent);
						}

						if(k[0].childNodes[2].text) 
							$("#quyen_u_table #select_r").attr("value",k[0].childNodes[2].text);
						else $("#quyen_u_table #select_r").attr("value",k[0].childNodes[2].textContent);

						if(k[0].childNodes[3].text) 
							$("#quyen_u_table #insert_r").attr("value",k[0].childNodes[3].text);
						else $("#quyen_u_table #insert_r").attr("value",k[0].childNodes[3].textContent);							

						if(k[0].childNodes[4].text) 
							$("#quyen_u_table #update_r").attr("value",k[0].childNodes[4].text);
						else $("#quyen_u_table #update_r").attr("value",k[0].childNodes[4].textContent);							

						if(k[0].childNodes[5].text) 
							$("#quyen_u_table #delete_r").attr("value",k[0].childNodes[5].text);
						else $("#quyen_u_table #delete_r").attr("value",k[0].childNodes[5].textContent);							
						
						$("#fquyen_update").dialog("option","disabled",false);
						$("#fquyen_update").dialog("option","position","center");
					}); //load
				}//success
	}); //ajax
}
/*--------------------------------------------------------------------------------------*/
function quyen_af_hk(e)
{
	var code=(e.keyCode ? e.keyCode : e.which);
	if(code==13)
	{
		$("#fquyen_add").dialog("option","buttons")["Thêm"]();
	}
	else if(code==27)
	{
		$("#fquyen_add").dialog("close");
	}
	e.stopPropagation();
	e.preventDefault();
}
/*--------------------------------------------------------------------------------------*/
function quyen_uf_hk(e)
{
	var code=(e.keyCode ? e.keyCode : e.which);
	if(code==13)
	{
		$("#fquyen_update").dialog("option","buttons")["Cập nhật"]();
	}
	else if(code==27)
	{
		$("#fquyen_update").dialog("close");
	}
	e.stopPropagation();
	e.preventDefault();
}
/*--------------------------------------------------------------------------------------*/
function quyen_af_ah()
{
	$(document).off("keydown");
	$(document).on("keydown",quyen_af_hk);
}
/*--------------------------------------------------------------------------------------*/
function quyen_af_dh()
{
	$(document).off("keydown");
	$(document).on("keydown",quyen_hk);
}
/*--------------------------------------------------------------------------------------*/
function quyen_uf_ah()
{
	$(document).off("keydown");
	$(document).on("keydown",quyen_uf_hk);
}
/*--------------------------------------------------------------------------------------*/
function quyen_uf_dh()
{
	$(document).off("keydown");
	$(document).on("keydown",quyen_hk);
}
/*--------------------------------------------------------------------------------------*/
function quyen_hk(e)
{
	var code=(e.keyCode ? e.keyCode : e.which);
	//Down
	if(code==38)
	{
		if($("#quyen_flex tr.trSelected").is("#quyen_flex tr:first")) return;
		var id=$("#quyen_flex tr.trSelected").prev().attr("id");
		$("#quyen_flex tr.trSelected").removeClass("trSelected");
		$("#quyen_flex #"+id).addClass("trSelected");
	}
	// Up
	else if(code==40)
	{
		if($("#quyen_flex tr.trSelected").is("#quyen_flex tr:last")) return;
		var id=$("#quyen_flex tr.trSelected").next().attr("id");
		$("#quyen_flex tr.trSelected").removeClass("trSelected");
		$("#quyen_flex #"+id).addClass("trSelected");
	}
	//Enter
	else if(code==13)
	{
		$("#fquyen").dialog("option", "buttons")["Sửa"]();
	}
	// Xóa
	else if(code==46)
	{
		$("#fquyen").dialog("option", "buttons")["Xoá"]();
	}
	// +
	else if(code==107)
	{
		$("#fquyen").dialog("option", "buttons")["Thêm"]();
	}
	e.stopPropagation();
	e.preventDefault();
}
/*--------------------------------------------------------------------------------------*/
function quyen_ah()
{
	$(document).off("keydown");
	$(document).on("keydown",quyen_hk);
	if(event!=null) if(typeof(event)!="undefined")
	{
		event.stopPropagation();
		event.preventDefault();
	}
}
/*--------------------------------------------------------------------------------------*/
function quyen_dh()
{
	$(document).off("keydown");
}
//-----------------------------------------------------------------------------------------
function quyen()
{
	if(!$("#account_flex .trSelected").attr("id"))
	{
		msgbox("fail","Phải chọn một mục để xem !");	
		return;
	}

	var quyenmagrp=$("#account_flex .trSelected > td:nth-child(3) > div ").html();
	var quyentengroups=$("#account_flex .trSelected > td:nth-child(4) > div ").html();
	
	quyenun=$("#account_flex .trSelected").attr("id");
	quyenun=quyenun.substring(quyenun.lastIndexOf("row")+3);

	if(quyenmagrp!="" && quyenmagrp!="&nbsp;") 
	{
		msgbox("fail","Người dùng "+quyenun+" đang thuộc nhóm "+quyentengroups+"!");	
		return;
	}
	
	if($("#fquyen").is(":data(dialog)")) return;
	$("body").append("<div id=\"fquyen\" style=\"display:none;\"></div>");
	
	var fquyenoptions={
		width:520,
		disabled:true,
		title:"Quyền hạn của người dùng ["+quyenun+"]",
		buttons:{
			"Sửa":quyen_uf,
		},
		modal:true,
		focus:quyen_ah,
		open:rebuildACL,
		close:function(){
			$("#fquyen").remove();
			quyen_dh();
		}
	};
	$("#fquyen").dialog(fquyenoptions);
	$("#fquyen").load("modules/quyen/g.php?un="+encodeURI(quyenun),"",function(){
		$("#fquyen").dialog("option","disabled",false);
		$("#fquyen").dialog("option","position","center");
	});
}
</script>
